﻿using DemountCIM.Framework;
using DemountCIM.Framework.DataBase;
using DemountCIM.Framework.DataBase.Table;
using Newtonsoft.Json.Bson;
using Prism.Commands;
using Prism.Mvvm;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemountCIM.Windows
{
    public class OptionWindowModel : BindableBase
    {
        private List<OptionModule> module;

        private DelegateCommand _changeValue;


        public OptionWindowModel()
        {
            module = new List<OptionModule>();

            var parameterRepository = DataBaseContainer.GetInstance().GetParameterRepository();
            parameterRepository.AsQueryable()
                .GroupBy(a => a.RunId)
                .Select(a => a.RunId)
                .ToList()
                .ForEach(a =>
                {
                    switch (a)
                    {
                        case 1:
                            module.Add(new OptionModule { RunId = a, RunName = "常规" });
                            break;
                        case 3:
                            module.Add(new OptionModule { RunId = a, RunName = "高级" });
                            break;
                    }

                });

            foreach (var module in module)
            {

                module.OptionParamList = parameterRepository.AsQueryable()
                    .Where(a => a.RunId == module.RunId)
                    .OrderBy(a => a.Sort)
                    .Select(a => new OptionParam
                    {
                        Id = a.Id,
                        //RunId = a.RunId,
                        ParamKey = a.ParamKey,
                        ParamName = a.ParamName,
                        ParamType = a.ParamType,
                        ParamValue = a.ParamValue,
                        ReadOnly = a.ReadOnly,
                        UserLevel = a.UserLevel,
                        Description = a.Description
                    }).ToList();
            }

        }

        public List<OptionModule> Module
        {
            get { return module; }
            set
            {
                SetProperty(ref module, value);
            }
        }

        public DelegateCommand ChangeValue
        {
            get
            {
                if (_changeValue == null)
                    _changeValue = new DelegateCommand(DoChangeValue);
                return _changeValue;
            }
            set { _changeValue = value; }
        }

        private void DoChangeValue()
        {
            foreach (var module in Module)
            {
                foreach (var item in module.OptionParamList)
                {
                    var rep = DataBaseContainer.GetInstance().GetParameterRepository();
                    switch (item.ParamType)
                    {
                        case ParamType.Int:
                            rep.WriteInt(module.RunId, item.ParamKey, Convert.ToInt32(item.ParamValue));
                            break;
                        case ParamType.Bool:
                            rep.WriteBool(module.RunId, item.ParamKey, Convert.ToBoolean(item.ParamValue));
                            break;
                        case ParamType.String:
                            rep.WriteString(module.RunId, item.ParamKey, item.ParamValue);
                            break;
                    }

                }
            }
            MachineCtrl.GetInstance().ReadDBParam();
        }

        #region 参数模型

        public class OptionModule : BindableBase
        {
            private int runId;
            private string runName;
            private List<OptionParam> optionParams;

            public int RunId { get { return runId; } set { SetProperty(ref runId, value); } }

            public string RunName { get { return runName; } set { SetProperty(ref runName, value); } }


            public List<OptionParam> OptionParamList { get { return optionParams; } set { SetProperty(ref optionParams, value); } }
        }

        public class OptionParam : BindableBase
        {
            private int id;
            //private int runId;
            private string paramKey;
            private string paramName;
            private string paramValue;
            private string description;
            private ParamType paramType;
            private UserLevel userLevel;
            private bool readOnly;

            public int Id { get { return id; } set { SetProperty(ref id, value); } }

            //public int RunId { get { return runId; } set { SetProperty(ref runId, value); } }

            public string ParamKey { get { return paramKey; } set { SetProperty(ref paramKey, value); } }

            public string ParamName { get { return paramName; } set { SetProperty(ref paramName, value); } }

            public string ParamValue
            {
                get { return paramValue; }
                set
                {
                    SetProperty(ref paramValue, value);

                }
            }

            public string Description { get { return description; } set { SetProperty(ref description, value); } }

            public ParamType ParamType { get { return paramType; } set { SetProperty(ref paramType, value); } }

            public UserLevel UserLevel { get { return userLevel; } set { SetProperty(ref userLevel, value); } }

            public bool ReadOnly { get { return readOnly; } set { SetProperty(ref readOnly, value); } }
        }
        #endregion

    }
}
